#!/usr/bin/env bash

###############################################################################
# Copyright (c) 2021 congzhang Technologies. All rights reserved.
###############################################################################

ROOT_DIR=$(realpath "$(dirname "${BASH_SOURCE[0]}")"/..)

function info() {
  echo -e "\033[034mInfo:\033[0m $*"
}

function warn() {
  >&2 echo -e "\033[093mWarning: $*\033[0m"
}

function error() {
  >&2 echo -e "\033[091mERROR: $*\033[0m"
}

function build_foonathan() {
  cd "$TEMP_BUILD_DIR"
  git clone https://github.com/foonathan/memory.git
  cd memory
  git checkout v0.7
  mkdir build
  cd build
  NODE_SIZES_FILE="/code/cross_compile/container_node_sizes_impl_linux_s32g.hpp"
  if [ ! -e "$NODE_SIZES_FILE" ]; then
    error "container_node_sizes_impl_linux_s32g.hpp doesn't exist!"
    exit 1
  fi
  cp "$NODE_SIZES_FILE" container_node_sizes_impl.hpp
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_dds_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    ..
  # NOTE: The -DFOONATHAN_MEMORY_CONTAINER_NODE_SIZES_IMPL is only for QNX
  #   platform. The actual container_node_sizes_impl.hpp that
  #   gets installed in the system does not have node sizes and only contains
  #   #define FOONATHAN_MEMORY_NO_NODE_SIZE 1. This in turn sets
  #   FOONATHAN_MEMORY_NO_NODE_SIZE in the FastDDS build process, causing
  #   FastDDS to use custome node size implementation. This is causing segfault
  #   due to incorrect node size during runtime. Manually copy the file.
  cp "$NODE_SIZES_FILE" src/container_node_sizes_impl.hpp
  if ! make -j $(nproc); then
    error "Failed to build foonathan!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy

  info "Built foonathan memory"
  cd "$TEMP_BUILD_DIR"
  rm -rf memory
}

function build_fastdds() {
  cd "$TEMP_BUILD_DIR"
  git clone --single-branch --branch v2.6.1-congzhang-patch --depth 1 https://code.congzhang.ds/third-party-library/eProsima/Fast-DDS.git
  # git clone --single-branch --branch v2.6.1 --depth 1 https://github.com/eProsima/Fast-DDS.git
  cd Fast-DDS
  # git checkout v2.6.1
  # The patch is to make sure foonathan node sizes are used, compilation stops
  #  otherwise.
  git apply /code/cross_compile/fastdds.patch
  mkdir -p build
  cd build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_dds_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DTHIRDPARTY=FORCE \
    -DNO_TLS=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    ..
  if ! make -j $(nproc); then
    error "Failed to build fastdds!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built fastdds"
}

function build_protobuf() {
  cd "$TEMP_BUILD_DIR"
  git clone https://github.com/protocolbuffers/protobuf.git
  cd protobuf
  git checkout v3.15.5
  cd cmake && mkdir build && cd build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -Dprotobuf_WITH_ZLIB=OFF \
    -Dprotobuf_BUILD_TESTS=OFF \
    ..
  if ! make -j $(nproc); then
    error "Failed to build protobuf!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  ldconfig
  info "Built protobuf"
}

function build_gflags() {
  cd "$TEMP_BUILD_DIR"
  git clone https://github.com/gflags/gflags.git
  cd gflags
  git checkout v2.2.2
  mkdir build && cd build
  cmake \
    -DARCH="$ARCH" \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
    ..
  if ! make -j $(nproc); then
    error "Failed to build gflags!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built gflags"
}

function build_gtest() {
  cd "$TEMP_BUILD_DIR"
  git clone https://github.com/google/googletest.git
  cd googletest
  git checkout release-1.10.0
  mkdir build && cd build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
    ..
  make -j $(nproc)
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built gtest"
}

function build_glog() {
  cd "$TEMP_BUILD_DIR"
  git clone https://github.com/google/glog.git
  cd glog
  git checkout v0.4.0
  git apply /code/cross_compile/glog_security.patch
  mkdir build
  cd build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
    ..
  if ! make -j "$(nproc)"; then
    error "Failed to build glog!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built glog"
}

function build_abseil() {
  cd "$TEMP_BUILD_DIR"
  curl -sS -L --retry 3 -o abseil.tar.gz -O https://github.com/abseil/abseil-cpp/archive/20200923.3.tar.gz
  tar xf abseil.tar.gz
  rm abseil.tar.gz
  mv abseil-cpp-20200923.3 abseil-cpp
  mkdir -p abseil-cpp/build
  cd abseil-cpp/build
  git checkout v0.4.0
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
    -D CMAKE_RULE_MESSAGES=OFF \
    -D CMAKE_POSITION_INDEPENDENT_CODE=ON \
    ..
  if ! make -s -j "$(nproc)"; then
    error "Failed to build abseil!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built abseil"
}

function build_ncurses() {
  cd "$TEMP_BUILD_DIR"
  curl -sS -L -O https://ftp.gnu.org/pub/gnu/ncurses/ncurses-6.1.tar.gz
  tar -xzvf ncurses-6.1.tar.gz
  cd ncurses-6.1
  if [ "$ARCH" == "aarch64" ]; then
    ./configure \
      --host=x86_64-linux-gnu \
      CC="/usr/bin/aarch64-linux-gnu-gcc-10" \
      CXX="/usr/bin/aarch64-linux-g++-10" \
      CPPFLAGS="-fPIC" \
      CXXFLAGS="-fPIC" \
      --disable-stripping \
      --with-pic \
      --with-shared \
      --without-ada \
      --disable-big-core \
      --enable-termcap \
      --enable-echo \
      --with-fallbacks=vt100 \
      --without-xterm-new \
      --prefix="$INSTALL_PATH"
  else
    ./configure \
      --host=x86_64-linux-gnu \
      CC="/usr/bin/gcc-10" \
      CXX="/usr/bin/g++-10" \
      CPPFLAGS="-fPIC" \
      CXXFLAGS="-fPIC" \
      --disable-stripping \
      --with-pic \
      --with-shared \
      --without-ada \
      --disable-big-core \
      --enable-termcap \
      --enable-echo \
      --with-fallbacks=vt100 \
      --without-xterm-new \
      --prefix="$INSTALL_PATH"
  fi
  if ! make -j $(nproc); then
    error "Failed to build ncurses!"
    exit 1
  fi

  if ! make install; then
    error "Failed to install ncurses!"
  fi
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built ncurses"
}

function build_eigen() {
  cd "$TEMP_BUILD_DIR"
  curl -sS -L --retry 3 -O https://gitlab.com/libeigen/eigen/-/archive/3.3.7/eigen-3.3.7.tar.bz2
  tar xvf eigen-3.3.7.tar.bz2
  mv eigen-3.3.7 eigen
  mkdir eigen/build
  cd eigen/build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
    ..
  if ! make -j "$(nproc)"; then
    error "Failed to build eigen!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built eigen"
}

function build_jsoncpp() {
  cd "$TEMP_BUILD_DIR"
  git clone https://github.com/open-source-parsers/jsoncpp.git
  mkdir -p jsoncpp/build
  cd jsoncpp/build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DJSONCPP_WITH_TESTS=OFF \
    ..

  if ! make jsoncpp_lib -j "$(nproc)"; then
    error "Failed to build jsoncpp_lib!"
    exit 1
  fi  
    
  make install

  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built jsoncpp"
}

function build_static_math() {
  cd "$TEMP_BUILD_DIR"
  git clone https://github.com/Morwenn/static_math.git
  mkdir -p static_math/build
  cd static_math
  git reset 764afd7ce246605d1b8be8a622b5a16f7803b26a --hard
  cd build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_INCLUDEDIR="$INSTALL_PATH/include" \
    ..

  if ! make -j "$(nproc)"; then
    error "Failed to build static_math_lib!"
    exit 1
  fi  
    
  make install

  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built static_math"
}

function build_boost() {
  cd "$TEMP_BUILD_DIR"
  
  curl -sS -L --retry 3 -O http://sourceforge.net/projects/boost/files/boost/1.81.0/boost_1_81_0.tar.bz2
  tar xf boost_1_81_0.tar.bz2
  cd boost_1_81_0
  
  ./bootstrap.sh --with-python=/usr/bin/python3 --with-libraries=container,timer,thread,math,filesystem,log --prefix="$INSTALL_PATH" 

  if [ "$ARCH" == "aarch64" ]; then
    
    mfile=project-config.jam

    if [ -e ${mfile} ];then
      mhost="/usr/bin/aarch64-linux-gnu-gcc-10 -fPIC"      
      sed -i "/using gcc/c using gcc : arm : ${mhost} ; " ${mfile}
    fi
  else
    mfile=project-config.jam
    mhost="/usr/bin/gcc-10 -fPIC"
    sed -i "/using gcc/c using gcc : arm : ${mhost} ; " ${mfile}

  fi
  
  if ! ./b2 -j "$(nproc)"; then
    error "Failed to build boost!"
    exit 1
  fi

  ./b2 install

  info "Built boost"
}

function build_boost_deploy() {
  cd "$TEMP_BUILD_DIR"
  
  curl -sS -L --retry 3 -O http://sourceforge.net/projects/boost/files/boost/1.81.0/boost_1_81_0.tar.bz2
  tar xf boost_1_81_0.tar.bz2
  cd boost_1_81_0
  
  ./bootstrap.sh --with-python=/usr/bin/python3 --with-libraries=container,timer,thread,math --prefix="/deploy/usr/aarch64-linux-gnu/local" 

  if [ "$ARCH" == "aarch64" ]; then
    
    mfile=project-config.jam

    if [ -e ${mfile} ];then
      mhost="/usr/bin/aarch64-linux-gnu-gcc-10 -fPIC"
      sed -i "/using gcc/c using gcc : arm : ${mhost} ; " ${mfile}
    fi
  else
    mfile=project-config.jam
    mhost="/usr/bin/gcc-10 -fPIC"
    sed -i "/using gcc/c using gcc : arm : ${mhost} ; " ${mfile}

  fi
  
  if ! ./b2 -j "$(nproc)"; then
    error "Failed to build boost!"
    exit 1
  fi

  ./b2 install

  info "Built boost deploy"
}


function build_zstd() {
  cd "$TEMP_BUILD_DIR"

  curl -sS -L --retry 3 -O https://github.com/facebook/zstd/releases/download/v1.5.0/zstd-1.5.0.tar.gz
  tar xf zstd-1.5.0.tar.gz
  cd zstd-1.5.0/build/cmake
  mkdir builddir
  cd builddir

  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
    ..

  if ! make  -j "$(nproc)"; then
    error "Failed to build jsoncpp_lib!"
    exit 1
  fi

  make install

  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built zstd"
}


function build_lz4() {

  cd "$TEMP_BUILD_DIR"
  curl -sS -L --retry 3 -o lz4.zip https://github.com/lz4/lz4/archive/v1.9.2.zip
  unzip -qq lz4.zip
  cd lz4-1.9.2/contrib/cmake_unofficial/
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \

  if ! make -j "$(nproc)"; then
    error "Failed to build eigen!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built lz4"

}

function build_yaml_cpp() {

  cd "$TEMP_BUILD_DIR"
  curl -sS -L --retry 3 -o yaml-cpp.zip -O https://github.com/jbeder/yaml-cpp/archive/33316d531bd9032d66f5bcc3ba1fd114a4ab0e1c.zip
  unzip yaml-cpp.zip
  rm yaml-cpp.zip
  mv yaml-cpp-33316d531bd9032d66f5bcc3ba1fd114a4ab0e1c yaml-cpp
  mkdir -p yaml-cpp/build
  cd yaml-cpp/build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DCMAKE_RULE_MESSAGES=OFF \
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    ..
  if ! make -j "$(nproc)"; then
    error "Failed to build yaml cpp!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built yaml cpp"

}


function build_ceres() {
  cd "$TEMP_BUILD_DIR"
  curl -sS -L --retry 3 -O http://ceres-solver.org/ceres-solver-1.14.0.tar.gz
  tar xvf ceres-solver-1.14.0.tar.gz
  rm ceres-solver-1.14.0.tar.gz
  mv ceres-solver-1.14.0 ceres
  mkdir ceres/build
  cd ceres/build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
  ..

  if ! make -j "$(nproc)"; then
    error "Failed to build ceres!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built ceres"
}

function build_opencv() {
  cd "$TEMP_BUILD_DIR"
  curl -sS -L --retry 3 -O https://github.com/opencv/opencv/archive/3.4.15.zip
  unzip -qq 3.4.15.zip
  rm -rf 3.4.15.zip

  curl -sS -L --retry 3 -O https://github.com/opencv/opencv_contrib/archive/3.4.15.zip
  unzip -qq 3.4.15.zip
  rm -rf 3.4.15.zip

  cd opencv-3.4.15
  mkdir -p build
  cd build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_BUILD_TYPE=MinSizeRel \
    -DBUILD_PROTOBUF=OFF \
    -DPROTOBUF_UPDATE_FILES=YES \
    -DOPENCV_EXTRA_MODULES_PATH=$TEMP_BUILD_DIR/opencv_contrib-3.4.15/modules \
    -DWITH_CUDA=OFF \
    -DWITH_NVCUVID=OFF \
    -DWITH_CUBLAS=1 \
    -DWITH_GTK=ON \
    -DWITH_GTK_2_X=ON \
    -DBUILD_DOCS=OFF \
    -DBUILD_PERF_TESTS=OFF \
    -DBUILD_TESTS=OFF \
    -DCMAKE_RULE_MESSAGES=OFF \
    -DENABLE_CCACHE=ON \
    -DBUILD_opencv_python2=Off \
  ..

  if ! make -j "$(nproc)"; then
    error "Failed to build opencv!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built opencv"
}

function build_pcl() {
  cd "$TEMP_BUILD_DIR"
  
  curl -sS -L --retry 3 -O https://github.com/PointCloudLibrary/pcl/archive/pcl-1.9.1.tar.gz
  tar xf pcl-1.9.1.tar.gz
  cd pcl-pcl-1.9.1
  patch -p1 < /code/cross_compile/pcl_boost_endian.patch
  mkdir build
  cd build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_CXX_FLAGS="-mavx2" \
    -DCMAKE_RULE_MESSAGES=OFF \
  ..

  if ! make -j "$(nproc)"; then
    error "Failed to build pcl!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built pcl"
}

function build_flann() {
  cd "$TEMP_BUILD_DIR"
  curl -sS -L --retry 3 -O https://github.com/flann-lib/flann/archive/refs/tags/1.8.4.tar.gz
  tar -xzf 1.8.4.tar.gz
  # issue src/cpp/CMakeLists.txt line #32: add_library(flann_cpp SHARED "")
  # The Cmake >= 3.11 does not support empty add_library
  # Create empty file to fix
  cd flann-1.8.4
  touch src/cpp/empty.cpp
  sed -e 's/flann_cpp SHARED ""/flann_cpp SHARED "empty.cpp"/g' \
    -i src/cpp/CMakeLists.txt
  sed -e 's/flann SHARED ""/flann SHARED "empty.cpp"/g' \
    -i src/cpp/CMakeLists.txt
  mkdir -p build
  cd build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_MATLAB_BINDINGS=OFF \
    -DBUILD_TESTS=OFF \
  ..

  if ! make -j "$(nproc)"; then
    error "Failed to build flann!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built flann"
}

function build_yaml_cpp() {

  cd "$TEMP_BUILD_DIR"
  curl -sS -L --retry 3 -o yaml-cpp.zip -O https://github.com/jbeder/yaml-cpp/archive/33316d531bd9032d66f5bcc3ba1fd114a4ab0e1c.zip
  unzip yaml-cpp.zip
  rm yaml-cpp.zip
  mv yaml-cpp-33316d531bd9032d66f5bcc3ba1fd114a4ab0e1c yaml-cpp
  mkdir -p yaml-cpp/build
  cd yaml-cpp/build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DCMAKE_RULE_MESSAGES=OFF \
    -DCMAKE_POSITION_INDEPENDENT_CODE=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    ..
  if ! make -j "$(nproc)"; then
    error "Failed to build yaml cpp!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built yaml cpp"

}

function build_curlpp() {

  info "download openssl..."
  sudo apt-get update
  sudo apt-get install libcurl4-openssl-dev 
  cd "$TEMP_BUILD_DIR"
  curl -sS -L --retry 3 -O "https://github.com/jpbarrette/curlpp/archive/v0.8.1.tar.gz"
  tar xf v0.8.1.tar.gz
  mkdir -p curlpp-0.8.1/build
  cd curlpp-0.8.1/build
  cmake \
    -DARCH="$ARCH" \
    -DCMAKE_TOOLCHAIN_FILE="/code/cross_compile/linux_toolchain.cmake" \
    -DCMAKE_INSTALL_PREFIX="$INSTALL_PATH" \
    -DBUILD_SHARED_LIBS=ON \
    ..

  if ! make -j "$(nproc)"; then
    error "Failed to build curlpp!"
    exit 1
  fi
  make install
  [ "$ARCH" == "aarch64" ] && make install DESTDIR=/deploy
  info "Built curlpp"

}

function build_gperftools(){
  if [ "$ARCH" == "x86_64" ]; then
    git clone https://github.com/gperftools/gperftools.git
    cd gperftools
    sh autogen.sh
    ./configure
    make all
    sudo make install
    info "Built gperftools cpp"
  fi
}

TEMP_BUILD_DIR=$(mktemp -d)
ARCH="aarch64"
INSTALL_PATH="/usr/aarch64-linux-gnu/local"
# build_curlpp
build_yaml_cpp
build_opencv
build_lz4
build_zstd
build_eigen
build_jsoncpp
build_static_math
build_boost
build_boost_deploy
build_foonathan
build_fastdds
build_protobuf
build_gflags
build_gtest
build_glog
# build_flann
# build_pcl
build_ceres
build_abseil
build_ncurses
rm -rf "$TEMP_BUILD_DIR"

TEMP_BUILD_DIR=$(mktemp -d)
ARCH="x86_64"
INSTALL_PATH="/usr/local"
build_curlpp
build_yaml_cpp
build_opencv
build_lz4
build_zstd
build_eigen
build_jsoncpp
build_static_math
build_boost
build_foonathan
build_fastdds
build_protobuf
build_gflags
build_gtest
build_glog
build_gperftools
# build_flann
# build_pcl
build_ceres
build_abseil
build_ncurses
rm -rf "$TEMP_BUILD_DIR"

exit 0
